let map = new AMap.Map('container', {
    viewMode: '2D',  // 默认使用 2D 模式
    zoom: 12, //初始化地图层级
    center: [104.066143, 30.673095]  //初始化地图中心点
});
const poi_size = 25;
const car_size = 45;
const icon = {
    parkingLot: new AMap.Icon({
        size: new AMap.Size(poi_size, poi_size), //图标尺寸
        image: "img/3parking_lot.png", //Icon 的图像
        imageOffset: new AMap.Pixel(0, 0), //图像相对展示区域的偏移量，适于雪碧图等
        imageSize: new AMap.Size(poi_size, poi_size), //根据所设置的大小拉伸或压缩图片
    }),
    carService: new AMap.Icon({
        size: new AMap.Size(poi_size, poi_size), //图标尺寸
        image: "img/service.png", //Icon 的图像
        imageOffset: new AMap.Pixel(0, 0), //图像相对展示区域的偏移量，适于雪碧图等
        imageSize: new AMap.Size(poi_size, poi_size), //根据所设置的大小拉伸或压缩图片
    }),
    factory: new AMap.Icon({//工厂图标
        size: new AMap.Size(poi_size, poi_size), //图标尺寸
        image: "img/factory.png", //Icon 的图像
        imageOffset: new AMap.Pixel(0, 0), //图像相对展示区域的偏移量，适于雪碧图等
        imageSize: new AMap.Size(poi_size, poi_size), //根据所设置的大小拉伸或压缩图片
    }),
    gasStation: new AMap.Icon({//加油站图标
        size: new AMap.Size(poi_size, poi_size), //图标尺寸
        image: "img/fuel_station.png", //Icon 的图像
        imageOffset: new AMap.Pixel(0, 0), //图像相对展示区域的偏移量，适于雪碧图等
        imageSize: new AMap.Size(poi_size, poi_size), //根据所设置的大小拉伸或压缩图片
    }),
    // car: new Amap.Icon({//汽车图标(已废弃，使用customVehicleIcon)
    //     size: new AMap.Size(20, 20), //图标尺寸
    //     image: "https://s11.ax1x.com/2024/01/07/piz7JUg.png", //Icon 的图像
    //     imageOffset: new AMap.Pixel(0, 0), //图像相对展示区域的偏移量，适于雪碧图等
    //     imageSize: new AMap.Size(20, 20), //根据所设置的大小拉伸或压缩图片
    // }),
    customVehicleIcon: `<div class="customVehicleIcon" style="opacity: 0.5;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;">
             <img src="img/lorry.png" height="${car_size}" width="${car_size}">
             </div>`, // customVehicleIcon半透明，customVehicleIcon2不透明
    customVehicleIcon2: `<div class="customVehicleIcon" style="-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;">
             <img src="img/lorry.png" height="${car_size}" width="${car_size}">
             </div>`
};
let markers = {
    pois: [],
    cars: []
};
let carMarkerTable = {};
let poiMarkerTable = {};
function getInitData() {
    return $.ajax({
        url: '/api/map',
        type: 'GET',
    });
}

getInitData().done(data => {
    let poiData = data[0];
    let carData = data[1];
    for(let i=0;i<poiData.length;i++){
        let poi = poiData[i];
        let tmp;
        switch (poi.poi_type) {
            case '工厂':
                tmp=icon.factory;
                break;
            case '加油站':
                tmp=icon.gasStation;
                break;
            case '汽车服务':
                tmp=icon.carService;
                break;
            case '停车场':
                tmp=icon.parkingLot;
                break;
        }
        let marker = new AMap.Marker({
            map: map,
            position: [poi.poi_lng, poi.poi_lat],
            title: poi.poi_name,
            icon: tmp
        });
        poiMarkerTable[poi.poi_id]=marker;//用于将poi_id与marker绑定
        markers.pois.push(marker);
    }
    for(let i=0;i<carData.length;i++){
        let car = carData[i];
        let marker = new AMap.Marker({
            map: map,
            position: [car.poi_lng, car.poi_lat],
            title: car.type_name,
            content: icon.customVehicleIcon, // customVehicleIcon半透明，customVehicleIcon2不透明
            offset: new AMap.Pixel(-25, -25)
        });
        markers.cars.push(marker);
        carMarkerTable[car.car_id]=marker; //用于将car_id与marker绑定
    }
    map.add(markers.pois);
    map.add(markers.cars);
});

